import unittest
import datetime

from meercat.base.instance import Instance
from meercat.base.metadata import Metadata
from meercat.base.resource import Resource

class ResourceTest(unittest.TestCase):
    def setUp(self):
        self.metadata = [Metadata(source='testS',format='testF',value='testV1'),
                         Metadata(source='testS',format='testF',value='testV2'),
                         Metadata(source='testS',format='testF',value='testV3'),
                         Metadata(source='testS',format='testF',value='testV4'),
                         Metadata(source='testS',format='testF',value='testV4')]
        
        self.modified = datetime.datetime(2009,8,1)
        self.last_modified = datetime.datetime(2009,8,10)
        self.vlast_modified = datetime.datetime(2009,9,1)
        
        self.instances=[Instance(source='testS',
                                 identifier='test1',
                                 active=True,
                                 metadata=self.metadata,
                                 modified=self.modified),
                        Instance(source='testS',
                                 identifier='test2',
                                 active=True,
                                 metadata=self.metadata,
                                 modified=self.modified),
                        Instance(source='testS',
                                 identifier='test2',
                                 active=True,
                                 metadata=self.metadata,
                                 modified=self.modified),
                        Instance(source='testS',
                                 identifier='test3',
                                 active=True,
                                 metadata=self.metadata,
                                 modified=self.last_modified),
                        Instance(source='testS',
                                 identifier='test4',
                                 active=False,
                                 metadata=self.metadata,
                                 modified=self.vlast_modified)]
        
        self.resource = Resource(identifier='test1',instances=self.instances)
    
    def testAccessors(self):
        self.assertEqual(self.resource.getIdentifier(),'test1')
        for m in self.metadata:
            self.assert_(m in self.resource.getMetadata())
        
        for m in self.resource.getMetadata():
            self.assert_(m in self.metadata)
        
        for i in self.resource:
            self.assert_(i in self.instances)
        
        self.assert_(self.resource.isActive())
        
        self.assertEqual(len(self.resource),4)
        self.assertEqual(self.resource.getLastModified(),self.vlast_modified)
        
    
    def testBadInstance(self):
        self.assertRaises(TypeError, Resource, identifier='test1',
                          instances=['garbage'])
        
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ResourceTest))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())